package org.tong.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * @author
 * @date 2022-09-05 9:10
 */
public class MyClientDetailService implements ClientDetailsService {

    public static Map<String, ClientDetails> clientMap = new HashMap<>();

    @Autowired
    public void init(PasswordEncoder passwordEncoder) {
        BaseClientDetails clientDetails = new BaseClientDetails();
        clientDetails.setClientId("admin");
        clientDetails.setClientSecret(passwordEncoder.encode("admin123"));
        clientDetails.setScope(Arrays.asList("all"));
        clientDetails.setAuthorizedGrantTypes(Arrays.asList("authorization_code", "password", "refresh_token"));
        Set<String> uriSet = new HashSet<>();
        uriSet.add("http://www.baidu.com");
        clientDetails.setRegisteredRedirectUri(uriSet);
        clientMap.put(clientDetails.getClientId(), clientDetails);
    }

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
        return clientMap.get(clientId);
    }
}
